#ifndef _X_POLL_CONTROL_H_
#define _X_POLL_CONTROL_H_
#include <xpoll.h>
#include <FastDelegate.h>
#include <xdynamic_array.h>
#include <xthread.h>
namespace zdh
{
    template<class PollControl>
    class XPollObject
    {
    public:
        typedef fastdelegate::FastDelegate<void (PollControl * paramControl, poll::TPollFlag paramFlag)> TOnEvent;
    public:
        XPollObject()
        {}
        TOnEvent & getOnEvent()
        {
            return m_OnEvent;
        }
    private:
        TOnEvent m_OnEvent;
    };

    template<class Poll, class Service>
    class XPollControl : public XThread
    {
    public:
        XPollControl(XInt paramMaxEventCount = 1024)
            :m_InitFlag(false), m_EventList(NULL), m_MaxEventCount(paramMaxEventCount)
        {
            m_EventList = new poll::TPollEvent[paramMaxEventCount];
        }

        ~XPollControl()
        {
            Close();
            if (isNotNULL(m_EventList))
            {
                delete [] m_EventList;
                m_EventList = NULL;
            }
        }

        XInt Init(XInt paramMaxFD)
        {
            int iRet = ERR_OK;
            do
            {
                if(!m_InitFlag)
                {
                    iRet = -1;
                    break;
                }
                m_ControlLogic = new Service();
                if (m_ControlLogic == NULL)
                {
                    iRet = -4;
                    break;
                }
                iRet = m_Poll.Create(paramMaxFD);
                if (iRet != ERR_OK)
                {
                    iRet = -2;
                    break;
                }
                m_InitFlag = true;
                m_ControlLogic->Init(this);
                if (iRet != ERR_OK)
                {
                    Close();
                    iRet = -3;
                }
            }while(false);

            return iRet;
        }

        virtual void Execute()
        {
            try
            {
            }
            catch (std::exception & e)
            {
            	
            }
            catch (EException & e)
            {
            }
            catch (...)
            {
            }
        }

        void Close()
        {

        }
    private:
        bool m_InitFlag;
        poll::TPollEvent * m_EventList;
        XInt m_MaxEventCount;
        Poll m_Poll;
        Service m_Service;
    };
}
#endif
